
package com.afd.ozblip.activity;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.afd.ozblip.adapter.BusinessRecDishListAdapter;
import com.afd.ozblip.entity.BusinessCouponEntity;
import com.afd.ozblip.entity.BusinessEntity;
import com.afd.ozblip.entity.CouponEntity;
import com.afd.ozblip.entity.DishEntity;
import com.afd.ozblip.entity.ReviewEntity;
import com.afd.ozblip.entity.SignEntity;
import com.afd.ozblip.entity.VipEntity;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.net.HttpUtil;
import com.afd.ozblip.util.AsyncAction;
import com.afd.ozblip.util.AsyncLoadData;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.ParseJson;
import com.afd.ozblip.util.StringUtil;
import com.afd.ozblip.view.MMAlert;
import com.j256.ormlite.android.AndroidDatabaseConnection;
import com.j256.ormlite.dao.Dao;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class BusinessInfoActivity extends BaseActivity {

    /****************** 视图变量 **************************/
    private ImageView businessMapBtn;
    private ImageView businessTelBtn;

    private LinearLayout recommendDishLayout;
    private LinearLayout dishListLayout;
    private LinearLayout otherPhotoLayout;
    private RelativeLayout reviewListLayout;
    private RelativeLayout messageListLayout;
    private LinearLayout otherInfoLayout;
    private LinearLayout commonRight;
    private ImageView businessImage;
    private TextView businessTitle;
    private RatingBar businessStar;
    private TextView businessPrice;
    private TextView businessTaste;
    private TextView businessEnvironment;
    private TextView businessService;
    private LinearLayout addVipLayout;

    private TextView businessAddress;
    private TextView businessTel;
    // 最新推荐菜
    private TextView recDishCount;
    private GridView redDishGv;
    private TableLayout recDishTl;
    // 商家最新优惠券
    // private LinearLayout couponLayout;
    private RelativeLayout couponLayout;
    private TextView couponTitle;
    private TextView couponTipTv;

    // 商家最新评论
    private TextView nrUserNameTv;
    private TextView nrAddTimeTv;
    private RatingBar nrStarRb;
    private TextView nrContentTv;
    private TextView reviewTipTv;

    // 商家最新签到
    private ImageView nsSignImageIv;
    private TextView signCntTv;
    private TextView nsSignContentTv;
    private TextView nsSignTimeTv;
    private TextView signTipTv;

    private View otherView;
    // 其它
    private TextView sendToFriendTv;
    private TextView shareTv;
    private TextView errorTv;

    private Animation enterAnim;
    private Animation exitAnim;

    /****************** 数据变量 **************************/
    private BusinessEntity oldBusinessInfo;
    private BusinessEntity newBusinessInfo;
    private String businessId;
    // 父级类型名称
    private String parentClassName;
    // 子级类型名称
    private String childClassName;
    private OzBlipApplication myApp;
    private Dao<BusinessEntity, Integer> businessDao;
    private Map<String, Object> isCollectRes;
    /**
     * Tab文字
     */
    private TextView tabText[];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.business_info);
        // 启动返回功能
        toBack();
        // 寻找视图
        findViews();
        // 设置监听
        setListeners();
        // 获取参数
        getBundle();
        // 设置列表传过来的数据的视图
        setOldViews();
        // 获取商品详情数据
        requestBusinessInfoData();
        // 获取用户是否收藏此商家
        requestUserIsCollectData();

    }

    /**
     * 寻找视图
     */
    public void findViews() {
        // 初始化APP
        this.myApp = (OzBlipApplication) getApplication();
        try {
            this.businessDao = myApp.getDBHelper().getDao(BusinessEntity.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.businessImage = (ImageView) findViewById(R.id.iv_business_detail_image);
        this.businessTitle = (TextView) findViewById(R.id.tv_business_detail_title);
        this.businessStar = (RatingBar) findViewById(R.id.rb_business_detail_star);
        this.businessPrice = (TextView) findViewById(R.id.tv_business_detail_price);
        this.businessTaste = (TextView) findViewById(R.id.tv_business_detail_taste);
        this.businessEnvironment = (TextView) findViewById(R.id.tv_business_detail_environment);
        this.businessService = (TextView) findViewById(R.id.tv_business_detail_service);
        this.addVipLayout = (LinearLayout) findViewById(R.id.ll_business_detail_addVip);
        this.businessAddress = (TextView) findViewById(R.id.tv_business_detail_address);
        this.businessTel = (TextView) findViewById(R.id.tv_business_detail_tel);

        this.businessMapBtn = (ImageView) findViewById(R.id.iv_business_detail_mapBtn);
        this.businessTelBtn = (ImageView) findViewById(R.id.iv_business_detail_telBtn);
        this.recommendDishLayout = (LinearLayout) findViewById(R.id.ll_business_detail_recommendDish);
        this.dishListLayout = (LinearLayout) findViewById(R.id.ll_business_detail_dishList);
        this.otherPhotoLayout = (LinearLayout) findViewById(R.id.ll_business_detail_otherPhoto);
        this.reviewListLayout = (RelativeLayout) findViewById(R.id.rl_business_detail_reviewList);
        this.commonRight = (LinearLayout) findViewById(R.id.ll_common_right);
        // 最新推荐菜
        this.recDishCount = (TextView) findViewById(R.id.tv_business_detail_recDishCount);
        this.redDishGv = (GridView) findViewById(R.id.gv_business_detail_recDishList);
        this.recDishTl = (TableLayout) findViewById(R.id.tl_business_detail_recDishList);
        // 最新优惠活动
        this.couponLayout = (RelativeLayout) findViewById(R.id.rl_business_detail_coupon);
        this.couponTitle = (TextView) findViewById(R.id.tv_business_detail_coupon_title);
        this.couponTipTv = (TextView) findViewById(R.id.tv_business_detail_coupon_tip);
        // 商家最新评论
        this.nrUserNameTv = (TextView) findViewById(R.id.tv_business_detail_newReview_userName);
        this.nrAddTimeTv = (TextView) findViewById(R.id.tv_business_detail_newReview_addTime);
        this.nrStarRb = (RatingBar) findViewById(R.id.rb_business_detail_newReview_star);
        this.nrContentTv = (TextView) findViewById(R.id.tv_business_detail_newReview_content);
        this.reviewTipTv = (TextView) findViewById(R.id.tv_business_detail_review_tip);
        // 商家最新签到
        this.signCntTv = (TextView) findViewById(R.id.tv_business_detail_signCnt);
        this.nsSignImageIv = (ImageView) findViewById(R.id.iv_business_sign_image);
        this.nsSignContentTv = (TextView) findViewById(R.id.tv_business_sign_content);
        this.nsSignTimeTv = (TextView) findViewById(R.id.tv_business_sign_addTime);
        this.signTipTv = (TextView) findViewById(R.id.tv_business_detail_signed_tip);

        this.otherView = findViewById(R.id.popup_more);
        // 其它
        this.sendToFriendTv = (TextView) findViewById(R.id.tv_business_detail_sendToFriend);
        this.shareTv = (TextView) findViewById(R.id.tv_business_detail_share);
        this.errorTv = (TextView) findViewById(R.id.tv_business_detail_error);

        this.messageListLayout = (RelativeLayout) findViewById(R.id.rl_business_detail_messageList);
        this.otherInfoLayout = (LinearLayout) findViewById(R.id.ll_business_detail_otherInfo);
        tabText = new TextView[5];
        tabText[0] = (TextView) findViewById(R.id.tv_main_menu_text_home);
        tabText[1] = (TextView) findViewById(R.id.tv_main_menu_text_hot);
        tabText[2] = (TextView) findViewById(R.id.tv_main_menu_text_category);
        tabText[3] = (TextView) findViewById(R.id.tv_main_menu_text_like);
        tabText[4] = (TextView) findViewById(R.id.tv_main_menu_text_me);
        tabText[0].setTag(0);

        this.enterAnim = AnimationUtils.loadAnimation(this, R.anim.anim_enter);
        this.exitAnim = AnimationUtils.loadAnimation(this, R.anim.anim_exit);
    }

    private void setOldViews() {
        String bid = "";
        if (oldBusinessInfo != null) {
            String imageUrl = oldBusinessInfo.getSellerImg();
            if (StringUtil.isNullOrEmpty(imageUrl)) {
                this.businessImage.setImageDrawable(getResources().getDrawable(
                        R.drawable.default_pic));
            } else {
                LoadImage loadImage = new LoadImage(this);
                loadImage.addTask(Constants.WEB_IMAGE_PATH + imageUrl,
                        this.businessImage);
            }
            this.businessTitle.setText(oldBusinessInfo.getSellerName());
            this.businessPrice.setText(oldBusinessInfo.getPrice());
            this.businessAddress.setText(oldBusinessInfo.getSellerAddress());
            this.businessTel.setText(oldBusinessInfo.getSellerTel());
            this.businessStar.setRating(oldBusinessInfo.getSellerStar());
            if (oldBusinessInfo.isMemberCardFlg()) {
                // XXX 暂时不显示，功能未实现
                // this.addVipLayout.setVisibility(0);
            } else {
                this.addVipLayout.setVisibility(8);
            }
            if ("美食".equals(parentClassName) || "快餐".equals(childClassName)) {
                this.dishListLayout.setVisibility(0);
            }
            bid = oldBusinessInfo.getSellerId();
        } else {
            bid = this.businessId;
        }
    }

    /**
     * 设置监听
     */
    public void setListeners() {
        View.OnClickListener businessInfoClick = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {

                    case R.id.ll_business_detail_addVip:
                        Intent businessIntent = new Intent(
                                BusinessInfoActivity.this, VipDetailActivity.class);
                        // businessIntent.putExtra("vipInfo", vip);
                        startActivity(businessIntent);
                        break;
                    case R.id.iv_business_detail_mapBtn:
                        Intent mapIntent = new Intent(BusinessInfoActivity.this,
                                BusinessMapActivity.class);
                        mapIntent.putExtra("businessName",
                                oldBusinessInfo.getSellerName());
                        mapIntent.putExtra("latitude",
                                oldBusinessInfo.getSellerLongitude());
                        mapIntent.putExtra("longitude",
                                oldBusinessInfo.getSellerDimension());
                        startActivity(mapIntent);
                        break;
                    case R.id.iv_business_detail_telBtn:
                        Intent telIntent = new Intent(Intent.ACTION_DIAL,
                                Uri.parse("tel:" + oldBusinessInfo.getSellerTel()));
                        startActivity(telIntent);
                        break;
                    case R.id.rl_business_detail_coupon:
                        Intent couponIntent = new Intent(BusinessInfoActivity.this,
                                CouponDetailActivity.class);
                        CouponEntity coupon = new CouponEntity();
                        coupon.setCouponId(newBusinessInfo.getNewsCoupon()
                                .getCouponId());
                        coupon.setBusinessName(newBusinessInfo.getSellerName());
                        coupon.setCouponTitle(newBusinessInfo.getNewsCoupon()
                                .getCouponTitle());
                        coupon.setCouponContent(newBusinessInfo.getNewsCoupon()
                                .getCouponNaiyo());
                        coupon.setLiftDate(newBusinessInfo.getNewsCoupon()
                                .getCouponTo());
                        couponIntent.putExtra("coupon", coupon);
                        startActivity(couponIntent);
                        break;
                    case R.id.ll_business_detail_recommendDish:
                        Intent recommendIntent = new Intent(
                                BusinessInfoActivity.this,
                                RecommendDishActivity.class);
                        recommendIntent.putExtra("businessId",
                                oldBusinessInfo.getSellerId());
                        startActivity(recommendIntent);
                        break;
                    case R.id.ll_business_detail_dishList:
                        Intent dishListIntent = new Intent(
                                BusinessInfoActivity.this,
                                BusinessDishActivity.class);
                        dishListIntent.putExtra("businessId",
                                oldBusinessInfo.getSellerId());
                        dishListIntent.putExtra("businessName",
                                oldBusinessInfo.getSellerName());
                        startActivity(dishListIntent);
                        break;
                    case R.id.ll_business_detail_otherPhoto:
                        Intent ohterPhotoIntent = new Intent(
                                BusinessInfoActivity.this,
                                BusinessPhotoActivity.class);
                        ohterPhotoIntent.putExtra("businessId",
                                oldBusinessInfo.getSellerId());
                        ohterPhotoIntent.putExtra("businessName",
                                oldBusinessInfo.getSellerName());
                        if (StringUtil.isNullOrEmpty(parentClassName)) {
                            ohterPhotoIntent.putExtra("parentClassName",
                                    newBusinessInfo.getParClass());
                        } else {
                            ohterPhotoIntent.putExtra("parentClassName",
                                    parentClassName);
                        }
                        if (StringUtil.isNullOrEmpty(childClassName)) {
                            ohterPhotoIntent.putExtra("childClassName",
                                    newBusinessInfo.getChiClass());
                        } else {
                            ohterPhotoIntent.putExtra("childClassName",
                                    childClassName);
                        }
                        startActivity(ohterPhotoIntent);
                        break;
                    case R.id.rl_business_detail_reviewList:
                        Intent reviewListIntent = new Intent(
                                BusinessInfoActivity.this,
                                BusinessReviewListActivity.class);
                        reviewListIntent.putExtra("businessId",
                                oldBusinessInfo.getSellerId());
                        reviewListIntent.putExtra("businessName",
                                oldBusinessInfo.getSellerName());
                        startActivity(reviewListIntent);
                        break;
                    case R.id.rl_business_detail_messageList:
                        Intent messageListIntent = new Intent(
                                BusinessInfoActivity.this,
                                BusinessMessageListActivity.class);
                        messageListIntent.putExtra("businessId",
                                oldBusinessInfo.getSellerId());
                        messageListIntent.putExtra("businessName",
                                oldBusinessInfo.getSellerName());
                        startActivity(messageListIntent);
                        break;
                    case R.id.ll_business_detail_otherInfo:
                        Intent otherInfoIntent = new Intent(
                                BusinessInfoActivity.this,
                                BusinessOtherInfoActivity.class);
                        otherInfoIntent.putExtra("otherInfo",
                                newBusinessInfo.getOtherInfo());
                        startActivity(otherInfoIntent);
                        break;
                    case R.id.ll_common_right:
                        Intent nearByBusinessIntent = new Intent(
                                BusinessInfoActivity.this,
                                NearbyBusinessActivity.class);
                        nearByBusinessIntent.putExtra("parentClassName",
                                newBusinessInfo.getParClass());
                        nearByBusinessIntent.putExtra("childClassName",
                                newBusinessInfo.getChiClass());
                        startActivity(nearByBusinessIntent);
                        break;
                    case R.id.tv_main_menu_text_home:
                        int collectStatus = Integer.parseInt(tabText[0].getTag()
                                .toString());
                        if (collectStatus == 0) {
                            try {
                                // AndroidDatabaseConnection conn = new
                                // AndroidDatabaseConnection(
                                // myApp.getDBHelper().getWritableDatabase(),
                                // true);
                                // businessDao.setAutoCommit(conn, false);
                                // newBusinessInfo.setCollect(true);
                                // businessDao.create(newBusinessInfo);
                                // conn.commit(null);
                                // businessDao.commit(conn);

                            } catch (Exception e) {
                            }
                            requestAddCollectData();
                        } else {
                            try {
                                // businessDao.deleteById(Integer
                                // .valueOf(newBusinessInfo.getSellerId()));
                            } catch (Exception e) {
                            }
                            requestDeleteCollectData();
                        }
                        break;
                    case R.id.tv_main_menu_text_hot:
                        if (commonUserInfo == null) {
                            showLoginDialog();
                        } else {
                            Intent signedIntent = new Intent(
                                    BusinessInfoActivity.this,
                                    AddSignedActivity.class);
                            signedIntent.putExtra("businessId",
                                    oldBusinessInfo.getSellerId());
                            signedIntent.putExtra("businessName",
                                    oldBusinessInfo.getSellerName());
                            startActivity(signedIntent);
                        }
                        break;
                    case R.id.tv_main_menu_text_category:
                        if (commonUserInfo == null) {
                            showLoginDialog();
                        } else {
                            Intent uploadIntent = new Intent(
                                    BusinessInfoActivity.this,
                                    UploadPhotoActivity.class);
                            uploadIntent.putExtra("businessId",
                                    oldBusinessInfo.getSellerId());
                            if (StringUtil.isNullOrEmpty(parentClassName)) {
                                uploadIntent.putExtra("parentClassName",
                                        newBusinessInfo.getParClass());
                            } else {
                                uploadIntent.putExtra("parentClassName",
                                        parentClassName);
                            }
                            if (StringUtil.isNullOrEmpty(childClassName)) {
                                uploadIntent.putExtra("childClassName",
                                        newBusinessInfo.getChiClass());
                            } else {
                                uploadIntent.putExtra("childClassName",
                                        childClassName);
                            }
                            startActivity(uploadIntent);

                        }
                        break;
                    case R.id.tv_main_menu_text_like:
                        if (commonUserInfo == null) {
                            showLoginDialog();
                        } else {
                            Intent reviewIntent = new Intent(
                                    BusinessInfoActivity.this,
                                    AddReviewActivity.class);
                            reviewIntent.putExtra("businessId",
                                    oldBusinessInfo.getSellerId());
                            reviewIntent.putExtra("businessName",
                                    oldBusinessInfo.getSellerName());
                            startActivity(reviewIntent);
                        }
                        break;
                    case R.id.tv_main_menu_text_me:
                        if (otherView.getVisibility() == View.VISIBLE) {
                            otherView.startAnimation(exitAnim);
                            otherView.setVisibility(View.GONE);
                        } else {
                            otherView.startAnimation(enterAnim);
                            otherView.setVisibility(View.VISIBLE);
                        }
                        break;
                    case R.id.tv_business_detail_error:
                        otherView.startAnimation(exitAnim);
                        otherView.setVisibility(View.GONE);
                        MMAlert.showAlert(BusinessInfoActivity.this, "",
                                new String[] {
                                        "商户已关", "地理位置错误", "商户信息错误"
                                }, null,
                                new MMAlert.OnAlertSelectId() {

                                    @Override
                                    public void onClick(int whichButton) {
                                        switch (whichButton) {
                                            case 0:
                                                String content1 = "商户已关闭";
                                                if (oldBusinessInfo != null) {
                                                    content1 = oldBusinessInfo
                                                            .getSellerName() + "已关闭";
                                                } else if (newBusinessInfo != null) {
                                                    content1 = newBusinessInfo
                                                            .getSellerName() + "已关闭";
                                                }
                                                requestSendErrorData(content1, "1");
                                                break;
                                            case 1:
                                                String content2 = "";
                                                if (oldBusinessInfo != null) {
                                                    content2 = oldBusinessInfo
                                                            .getSellerLongitude()
                                                            + "@"
                                                            + oldBusinessInfo
                                                                    .getSellerDimension();
                                                } else if (newBusinessInfo != null) {
                                                    content2 = newBusinessInfo
                                                            .getSellerLongitude()
                                                            + "@"
                                                            + newBusinessInfo
                                                                    .getSellerDimension();
                                                }
                                                requestSendErrorData(content2, "2");
                                                break;
                                            case 2:
                                                Intent errorIntent = new Intent(
                                                        BusinessInfoActivity.this,
                                                        BusinessErrorActivity.class);
                                                errorIntent.putExtra("businessInfo",
                                                        newBusinessInfo);
                                                startActivity(errorIntent);
                                                break;
                                        }
                                    }

                                });
                        break;

                    default:
                        break;
                }
            }
        };
        this.addVipLayout.setOnClickListener(businessInfoClick);
        this.businessMapBtn.setOnClickListener(businessInfoClick);
        this.businessTelBtn.setOnClickListener(businessInfoClick);
        this.couponLayout.setOnClickListener(businessInfoClick);
        this.recommendDishLayout.setOnClickListener(businessInfoClick);
        this.dishListLayout.setOnClickListener(businessInfoClick);
        this.otherPhotoLayout.setOnClickListener(businessInfoClick);
        this.reviewListLayout.setOnClickListener(businessInfoClick);
        this.messageListLayout.setOnClickListener(businessInfoClick);
        this.otherInfoLayout.setOnClickListener(businessInfoClick);
        this.commonRight.setOnClickListener(businessInfoClick);
        this.sendToFriendTv.setOnClickListener(businessInfoClick);
        this.shareTv.setOnClickListener(businessInfoClick);
        this.errorTv.setOnClickListener(businessInfoClick);
        for (int i = 0; i < 5; i++) {
            tabText[i].setOnClickListener(businessInfoClick);
        }
    }

    /**
     * 获取传递的参数
     */
    private void getBundle() {
        Intent intent = getIntent();
        oldBusinessInfo = (BusinessEntity) intent
                .getSerializableExtra("businessInfo");
        businessId = intent.getStringExtra("businessId");
        parentClassName = intent.getStringExtra("parentClassName");
        childClassName = intent.getStringExtra("childClassName");
    }

    /**
     * 查询用户是否收藏此商家
     */
    private void requestUserIsCollectData() {
        if (commonUserInfo == null) {
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "", new AsyncAction() {

            @Override
            public void doLoad() throws Exception {
                // 获取提交参数
                Map<String, Object> businessInfoParamMap = new HashMap<String, Object>();
                businessInfoParamMap.put("userId", commonUserInfo.getUserId());
                if (oldBusinessInfo == null) {
                    businessInfoParamMap.put("sellerId", businessId);
                } else {
                    businessInfoParamMap.put("sellerId",
                            oldBusinessInfo.getSellerId());
                }
                // 发送请求
                byte[] bIsCollect = HttpUtil.requestByPost(
                        BusinessInfoActivity.this,
                        Constants.USER_IS_COLLECT_API, businessInfoParamMap);
                if (bIsCollect != null) {
                    isCollectRes = new ParseJson()
                            .parseAddResultJson(bIsCollect);
                }
            }

            @Override
            public void loadEnd() throws Exception {
                boolean returnFlg = (Boolean) isCollectRes.get("returnFlg");
                if (returnFlg) {
                    tabText[0].setText("取消收藏");
                    tabText[0].setTag(1);
                } else {
                    tabText[0].setText("收藏");
                    tabText[0].setTag(0);
                }
            }

        });
        ald.execute(0);
    }

    /**
     * 添加收藏数据
     */
    private void requestAddCollectData() {
        if (commonUserInfo == null) {
            showLoginDialog();
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "收藏中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> businessInfoParamMap = new HashMap<String, Object>();
                        businessInfoParamMap.put("userId",
                                commonUserInfo.getUserId());
                        if (oldBusinessInfo == null) {
                            businessInfoParamMap.put("sellerId", businessId);
                        } else {
                            businessInfoParamMap.put("sellerId",
                                    oldBusinessInfo.getSellerId());
                        }
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                BusinessInfoActivity.this,
                                Constants.USER_ADD_COLLECT_API,
                                businessInfoParamMap);
                        if (commentItemList != null) {
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        showShortToast(BusinessInfoActivity.this, "收藏成功");
                        tabText[0].setText("取消收藏");
                        tabText[0].setTag(1);
                    }

                });
        ald.execute(0);
    }

    /**
     * 取消收藏数据
     */
    private void requestDeleteCollectData() {
        if (commonUserInfo == null) {
            showLoginDialog();
            return;
        }
        AsyncLoadData ald = new AsyncLoadData(this, "取消收藏中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> businessInfoParamMap = new HashMap<String, Object>();
                        businessInfoParamMap.put("userId",
                                commonUserInfo.getUserId());
                        if (oldBusinessInfo == null) {
                            businessInfoParamMap.put("sellerId", businessId);
                        } else {
                            businessInfoParamMap.put("sellerId",
                                    oldBusinessInfo.getSellerId());
                        }
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                BusinessInfoActivity.this,
                                Constants.USER_DELETE_COLLECT_API,
                                businessInfoParamMap);
                        if (commentItemList != null) {
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        showShortToast(BusinessInfoActivity.this, "取消收藏成功");
                        tabText[0].setText("收藏");
                        tabText[0].setTag(0);
                    }

                });
        ald.execute(0);
    }

    /**
     * TODO 获取商品详情数据
     */
    private void requestBusinessInfoData() {

        AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> businessInfoParamMap = new HashMap<String, Object>();
                        if (oldBusinessInfo == null) {
                            businessInfoParamMap.put("sellerId", businessId);
                        } else {
                            businessInfoParamMap.put("sellerId",
                                    oldBusinessInfo.getSellerId());
                        }
                        businessInfoParamMap.put("language", commonLanguage);
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                BusinessInfoActivity.this,
                                Constants.SELLER_DETAIL_API,
                                businessInfoParamMap);
                        if (commentItemList != null) {
                            newBusinessInfo = new ParseJson()
                                    .parseBusinessInfoJson(commentItemList);
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                        setViews();
                    }

                });
        ald.execute(0);
    }

    /**
     * 报告商户错误数据
     */
    private void requestSendErrorData(final String content, final String type) {
        if (commonUserInfo == null) {
            showLoginDialog();
        }
        AsyncLoadData ald = new AsyncLoadData(this, "加载中...",
                new AsyncAction() {

                    @Override
                    public void doLoad() throws Exception {
                        // 获取提交参数
                        Map<String, Object> businessInfoParamMap = new HashMap<String, Object>();
                        if (oldBusinessInfo == null) {
                            businessInfoParamMap.put("sellerId", businessId);
                        } else {
                            businessInfoParamMap.put("sellerId",
                                    oldBusinessInfo.getSellerId());
                        }
                        businessInfoParamMap.put("userId",
                                commonUserInfo.getUserId());
                        businessInfoParamMap.put("content", content);
                        businessInfoParamMap.put("type", type);
                        // 发送请求
                        byte[] commentItemList = HttpUtil.requestByPost(
                                BusinessInfoActivity.this,
                                Constants.SELLER_ERROR_API,
                                businessInfoParamMap);
                        if (commentItemList != null) {
                        }
                    }

                    @Override
                    public void loadEnd() throws Exception {
                    }

                });
        ald.execute(0);
    }

    private void setViews() {
        if (newBusinessInfo != null) {
            // 将老对象替换为新对象
            oldBusinessInfo = newBusinessInfo;
            this.businessTitle.setText(newBusinessInfo.getSellerName());
            this.businessPrice.setText(newBusinessInfo.getPrice());
            this.businessAddress.setText(newBusinessInfo.getSellerAddress());
            this.businessTel.setText(newBusinessInfo.getSellerTel());
            BusinessCouponEntity coupon = newBusinessInfo.getNewsCoupon();
            if (coupon != null) {
                this.couponLayout.setVisibility(0);
                this.couponTipTv.setVisibility(8);
                this.couponTitle.setText(newBusinessInfo.getNewsCoupon()
                        .getCouponTitle());
            } else {
                this.couponLayout.setVisibility(8);
                // this.couponTipTv.setText(getResources().getString(
                // R.string.noCoupon_text));
                // this.couponTipTv.setVisibility(0);
            }
            if ("美食".equals(newBusinessInfo.getParClass())
                    || "快餐".equals(newBusinessInfo.getChiClass())) {
                this.dishListLayout.setVisibility(0);
                int rdCount = newBusinessInfo.getRecDishCount();
                if (rdCount == 0) {
                    this.recommendDishLayout.setVisibility(8);
                } else {
                    this.recommendDishLayout.setVisibility(0);
                    this.recDishCount.setText(String.valueOf(rdCount));
                    List<DishEntity> recDishList = newBusinessInfo
                            .getRecommendDish();
                    if (recDishList != null && recDishList.size() > 0) {
                        // BusinessRecDishListAdapter recDishAdapter = new
                        // BusinessRecDishListAdapter(
                        // this);
                        // recDishAdapter.addItems(recDishList);
                        // this.redDishGv.setAdapter(recDishAdapter);
                        setRecommendDishData(recDishList);
                    }
                }
            }
            ReviewEntity review = newBusinessInfo.getSellerTalkInfo();
            if (review != null) {
                this.reviewListLayout.setVisibility(0);
                this.reviewTipTv.setVisibility(8);
                this.nrUserNameTv.setText(review.getUserName());
                this.nrAddTimeTv.setText(review.getReviewDate());
                this.nrStarRb.setRating(review.getSellerStar());
                this.nrContentTv.setText(review.getReviewNaiyo());
            } else {
                this.reviewListLayout.setVisibility(8);
                // this.reviewTipTv.setText(getResources().getString(
                // R.string.noReview_text));
                // this.reviewTipTv.setVisibility(0);
            }
            this.signCntTv
                    .setText(String.valueOf(newBusinessInfo.getSginCnt()));
            SignEntity sign = newBusinessInfo.getSellerPoint();
            if (sign != null) {
                this.messageListLayout.setVisibility(0);
                this.signTipTv.setVisibility(8);
                this.nsSignContentTv.setText(sign.getSginNaiyo());
                this.nsSignTimeTv.setText(sign.getSginDate());
            } else {
                this.messageListLayout.setVisibility(8);
                // this.signTipTv.setText(getResources().getString(
                // R.string.noSign_text));
                // this.signTipTv.setVisibility(0);
            }
            // 加入浏览历史
            try {
                AndroidDatabaseConnection conn = new AndroidDatabaseConnection(
                        myApp.getDBHelper().getWritableDatabase(), true);
                businessDao.setAutoCommit(conn, false);
                businessDao.create(newBusinessInfo);
                conn.commit(null);
                businessDao.commit(conn);
            } catch (Exception e) {
            }
        }
    }

    void setRecommendDishData(List<DishEntity> recDishList) {
        int recDishSize = recDishList.size();
        TableRow tableRow = new TableRow(this);
        if (recDishSize < 3) {
            recDishTl.addView(tableRow);
        }
        for (int i = 0; i < recDishSize; i++) {
            if (i != 0 && i % 3 == 0) {
                tableRow = new TableRow(this);
                recDishTl.addView(tableRow);
            } else {
                DishEntity dish = recDishList.get(i);
                TextView textView = new TextView(this);
                int textPaddingRight = getResources().getDimensionPixelSize(
                        R.dimen.recommend_dish_margin_right);
                textView.setPadding(0, 0, textPaddingRight, 0);
                textView.setText(dish.getDishName());
                tableRow.addView(textView);
            }

        }
    }
}
